'use strict';

/* Controllers */

angular.module('ender.game.choice', ['ngResource', 'ender.game.services']).
    controller('ChoiceController', function ($scope, choiceRepository, gameStateService) {
        var rootChoice = choiceRepository.getRootChoice();

        $scope.currentChoice = rootChoice;

        $scope.setCurrentChoice = function (choice) {
            $scope.currentChoice = choice;
        };
        $scope.backToParentChoice = function () {
            $scope.currentChoice = $scope.currentChoice.parentChoice;
        };

        $scope.selectHero = function(hero){
            gameStateService.startGame(hero);
        };
    })
    .factory('choiceRepository', function ($resource) {
        var choiceRepository = {};
        var choiceResource = $resource('data/choices.json');

        function extractChoiceCategories(parentChoice, choiceResourceArray) {
            parentChoice.categories = [];

            for (var i = 0; i < choiceResourceArray.length; i++) {
                var categoryResource = choiceResourceArray[i];
                var category = {};
                category.name = categoryResource.name;
                category.image = categoryResource.image;
                category.description = categoryResource.description;
                category.disabled = categoryResource.disabled;
                category.visible = categoryResource.visible;

                category.parentChoice = parentChoice;

                if (categoryResource.final) {
                    category.final = categoryResource.final;

                    category.heroes = [];
                    for (var j = 0; j < categoryResource.heroes.length; j++) {
                        var heroResource = categoryResource.heroes[j];
                        category.heroes.push(heroResource);
                    }

                } else {
                    category.contains = categoryResource.contains;
                    extractChoiceCategories(category, categoryResource.categories);
                }

                parentChoice.categories.push(category);
            }
        }

        var choicesResource = choiceResource.get(function () {
            choiceRepository.contains = choicesResource.contains;
            extractChoiceCategories(choiceRepository, choicesResource.categories);
        });

        return {
            getRootChoice: function () {
                return choiceRepository;
            }
        };
    })
    .directive('endChoiceCategoryList', function factoryFunction() {
        return {
            restrict: 'E',
            replace: true,
            scope: {
                categories: "=",
                selectCategory: "&"
            },
            templateUrl: 'templates/category-list.html',
            link: function (scope, element, iAttrs) {
                scope.visibilityFilter = {
                    visible : true
                };
                scope.$watch('categories', function () {
                    if (scope.categories && scope.categories.length == 2) {
                        scope.largeItems = true;
                    }
                    $('body > div.qtip').remove();
                    element.find('li').qtip({
                        style: 'dark'
                    });
                });
            }
        }
    })
    .directive('endChoiceHeroList', function factoryFunction() {
        return {
            restrict: 'E',
            replace: true,
            scope: {
                heroes: "=",
                selectHero: "&"
            },
            templateUrl: 'templates/hero-list.html',
            link: function (scope, element, attrs) {
                $('body > div.qtip').remove();

                scope.selectedHero = scope.heroes[0];
                scope.setSelectedHero = function (hero) {
                    scope.selectedHero = hero;
                }
            }
        }
    });


